package com.example.jnidemo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.jnidemo.util.MD5Util;
import com.example.jnidemo.util.SharedPreferencesUtils;
import com.example.jnidemo.util.Tools;

public class RegisterActivity extends AppCompatActivity {

    private Toast toast = null;
    private EditText mEdtActiveCode;
    private TextView mTvCopy;
    private TextView mTvDeviceId;

    public static void start(Context context) {
        Intent starter = new Intent(context, RegisterActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
    }

    private void initView() {

        mTvDeviceId = findViewById(R.id.tv_device_id);
        mEdtActiveCode = findViewById(R.id.edt_active_code);
        mTvCopy = findViewById(R.id.tv_copy);

        mTvDeviceId.setText(Tools.getIMEI());
        ((TextView) findViewById(R.id.tv_version)).setText("版本号: " + BuildConfig.VERSION_NAME);
        mTvCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String deviceId = mTvDeviceId.getText().toString();
                if (TextUtils.isEmpty(deviceId)) {
                    showToast("设备ID获取失败");
                }
                Tools.copy(getApplicationContext(), deviceId);
                showToast("设备ID复制成功");
            }
        });
        findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String registerCode = mEdtActiveCode.getText().toString();
                if (TextUtils.isEmpty(registerCode)) {
                    showToast("请输入激活码");
                    return;
                }
                String deviceId = mTvDeviceId.getText().toString();
                if (TextUtils.isEmpty(deviceId)) {
                    showToast("设备ID获取失败");
                    return;
                }
                String activeCode = MD5Util.MD5(deviceId);
                if (TextUtils.equals(activeCode, registerCode)) {
                    showToast("验证成功");
                    SharedPreferencesUtils.setParam(getApplicationContext(), SPConfig.IS_LOGIN, "1");
                    MainActivity.start(RegisterActivity.this);
                    finish();
                } else {
                    showToast("激活码无效");
                }
            }
        });
    }

    private void showToast(String message) {
        if (toast == null) {
            toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT); // 正常执行
        } else {
            toast.setText(message); // 用于覆盖前面未消失的提示信息
        }
        toast.setGravity(Gravity.CENTER, 0, -30);// 居中显示
        toast.show();
    }
}